package com.guoyh.bilui.ext

import android.content.Context
import android.graphics.Color
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.viewpager2.widget.ViewPager2
import com.guoyh.bilui.tab.indicator.CLinePagerIndicator
import com.guoyh.bilui.tab.titleview.ScaleTransitionPagerTitleView
import com.guoyh.bilui.utils.UIUtils.getColorm
import com.guoyh.baseutils.appContext
import com.guoyh.baseutils.entity.common.CommonBean
import com.guoyh.baseutils.ext.dp
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView

/**
 * @CreateDate: 7/29/21 2:26 PM
 * @Author: guoyh
 * @Description: java类作用描述
 */

/**
 * 指示器 绑定viewPager2
 */


fun <T : CommonBean> MagicIndicator.bindViewPager2(
    viewPager: ViewPager2,
    mStringList: List<T> = arrayListOf(),
    action: (index: Int) -> Unit = {}
) {
    val commonNavigator = CommonNavigator(appContext)
    commonNavigator.scrollPivotX = 0.55f;
    commonNavigator.adapter = object : CommonNavigatorAdapter() {

        override fun getCount(): Int {
            return mStringList.size
        }

        override fun getTitleView(context: Context, index: Int): IPagerTitleView {
            return ScaleTransitionPagerTitleView(appContext).apply {
                //设置文本
                text = mStringList[index].getXTitle()
                //字体大小
                textSize = 17f
                //未选中颜色
                normalColor = Color.RED
                //选中颜色
                selectedColor = Color.BLUE

                //点击事件
                setOnClickListener {
                    viewPager.currentItem = index
                    action.invoke(index)
                }
            }
        }

        override fun getIndicator(context: Context): IPagerIndicator {
            return CLinePagerIndicator(context).apply {
                mode = CLinePagerIndicator.MODE_WRAP_CONTENT
                //线条的宽高度
                lineHeight = 3.dp.toFloat()
//                lineWidth = UIUtil.dip2px(appContext, 30.0).toFloat()
                //线条的圆角
                roundRadius = UIUtil.dip2px(appContext, 6.0).toFloat()

                startInterpolator = AccelerateInterpolator()

                endInterpolator = DecelerateInterpolator(2.0f)
                yOffset = 4.dp.toFloat()


                //线条的颜色
                setColors(getColorm("#5db5ff"))
            }
        }

    }
    navigator = commonNavigator



    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            this@bindViewPager2.onPageSelected(position)
            action.invoke(position)
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            this@bindViewPager2.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            this@bindViewPager2.onPageScrollStateChanged(state)
        }
    })


}